/*
 * Copyright 2016 Huawei Technologies Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.openo.sdno.ipsecservice.site2dctest;

import java.util.List;

import org.codehaus.jackson.type.TypeReference;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openo.baseservice.remoteservice.exception.ServiceException;
import org.openo.sdno.exception.HttpCode;
import org.openo.sdno.framework.container.util.JsonUtil;
import org.openo.sdno.ipsecservice.site2dcmocoserver.SbiAdapterSuccessServer;
import org.openo.sdno.ipsecservice.util.DriverRegisterManager;
import org.openo.sdno.overlayvpn.model.v2.ipsec.NbiIpSec;
import org.openo.sdno.testframework.checker.IChecker;
import org.openo.sdno.testframework.http.model.HttpModelUtils;
import org.openo.sdno.testframework.http.model.HttpRequest;
import org.openo.sdno.testframework.http.model.HttpResponse;
import org.openo.sdno.testframework.http.model.HttpRquestResponse;
import org.openo.sdno.testframework.replace.PathReplace;
import org.openo.sdno.testframework.testmanager.TestManager;
import org.openo.sdno.testframework.topology.ResourceType;
import org.openo.sdno.testframework.topology.Topology;

/**
 * ITOperateIpSecSuccess test class. <br>
 * 
 * @author
 * @version SDNO 0.5 June 16, 2016
 */
public class ITOperateIpSecSuccess extends TestManager {

    private static SbiAdapterSuccessServer sbiAdapterServer = new SbiAdapterSuccessServer();

    private static final String CREATE_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/createipsecsuccess1.json";

    private static final String QUERY_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/queryipsecsuccess1.json";

    private static final String BATCH_QUERY_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/batchqueryipsecsuccess1.json";

    private static final String UNDEPLOY_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/undeployipsecsuccess1.json";

    private static final String DEPLOY_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/deployipsecsuccess1.json";

    private static final String UPDATE_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/updateipsecsuccess1.json";

    private static final String DELETE_IPSEC_SUCCESS_TESTCASE =
            "src/integration-test/resources/site2dctestcase/deleteipsecsuccess1.json";

    private static final String TOPODATA_PATH = "src/integration-test/resources/site2dctopodata";

    private static Topology topo = new Topology(TOPODATA_PATH);

    @BeforeClass
    public static void setup() throws ServiceException {
        topo.createInvTopology();
        DriverRegisterManager.registerDriver();
        sbiAdapterServer.start();

    }

    @AfterClass
    public static void tearDown() throws ServiceException {
        sbiAdapterServer.stop();
        DriverRegisterManager.unRegisterDriver();
        topo.clearInvTopology();
    }

    @Test
    public void testOperateIpSecSuccess() throws ServiceException {

        // create
        HttpRquestResponse httpCreateObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(CREATE_IPSEC_SUCCESS_TESTCASE);
        HttpRequest createRequest = httpCreateObject.getRequest();
        List<NbiIpSec> newNbiData = JsonUtil.fromJson(createRequest.getData(), new TypeReference<List<NbiIpSec>>() {});
        newNbiData.get(0).setSrcNeId(topo.getResourceUuid(ResourceType.NETWORKELEMENT, "Ne1"));
        newNbiData.get(0).setDestNeId(topo.getResourceUuid(ResourceType.NETWORKELEMENT, "Ne2"));
        createRequest.setData(JsonUtil.toJson(newNbiData));
        execTestCase(createRequest, new SuccessChecker());

        // query
        HttpRquestResponse queryHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(QUERY_IPSEC_SUCCESS_TESTCASE);
        HttpRequest queryRequest = queryHttpObject.getRequest();
        queryRequest.setUri(PathReplace.replaceUuid("connectionid", queryRequest.getUri(), "ipsecconnection1id"));
        execTestCase(queryRequest, new SuccessChecker());

        // batch query
        HttpRquestResponse batchqueryHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(BATCH_QUERY_IPSEC_SUCCESS_TESTCASE);
        HttpRequest batchqueryRequest = batchqueryHttpObject.getRequest();
        execTestCase(batchqueryRequest, new SuccessChecker());

        // undeploy
        HttpRquestResponse undeployHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(UNDEPLOY_IPSEC_SUCCESS_TESTCASE);
        HttpRequest undeployReq = undeployHttpObject.getRequest();
        execTestCase(undeployReq, new SuccessChecker());

        // deploy
        HttpRquestResponse deployHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(DEPLOY_IPSEC_SUCCESS_TESTCASE);
        HttpRequest deployReq = deployHttpObject.getRequest();
        execTestCase(deployReq, new SuccessChecker());

        // update
        HttpRquestResponse updateHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(UPDATE_IPSEC_SUCCESS_TESTCASE);
        HttpRequest updateReq = updateHttpObject.getRequest();
        List<NbiIpSec> updateNbiData = JsonUtil.fromJson(updateReq.getData(), new TypeReference<List<NbiIpSec>>() {});
        updateNbiData.get(0).setSrcNeId(topo.getResourceUuid(ResourceType.NETWORKELEMENT, "Ne1"));
        updateNbiData.get(0).setDestNeId(topo.getResourceUuid(ResourceType.NETWORKELEMENT, "Ne2"));
        updateReq.setData(JsonUtil.toJson(updateNbiData));
        execTestCase(updateReq, new SuccessChecker());

        // undeploy and delete
        execTestCase(undeployReq, new SuccessChecker());
        HttpRquestResponse deleteHttpObject =
                HttpModelUtils.praseHttpRquestResponseFromFile(DELETE_IPSEC_SUCCESS_TESTCASE);
        HttpRequest deleteReq = deleteHttpObject.getRequest();
        deleteReq.setUri(PathReplace.replaceUuid("ipsecConnectionId", deleteReq.getUri(), "ipsecconnection1id"));
        execTestCase(deleteReq, new SuccessChecker());

    }

    private class SuccessChecker implements IChecker {

        @Override
        public boolean check(HttpResponse response) {
            if(HttpCode.isSucess(response.getStatus())) {
                return true;
            }

            return false;
        }
    }

}
